package com.cqc.web;

import com.cqc.common.Result;
import com.cqc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
 * 异步请求 callable
 **/
@Controller
@RequestMapping("/callable/")
public class CallableController {


    /**
     * http://localhost:8080/callable/async
     * controller返回Callable，必须配置 WebMvcConfigurer.configureAsyncSupport(..)然后指定异步线程池
     * 该案例同 UserController，Callable和 @async效果是一样的。我们发出4个请求，不会阻塞servlet容器(tomcat)，
     */
    @GetMapping("async")
    @ResponseBody
    public Callable<String> async() {
        System.out.println(" 主线程 threadId: " + Thread.currentThread().getId() + ",threadName: " + Thread.currentThread().getName());

        return () -> {
            //模拟Service执行耗时操作
            TimeUnit.SECONDS.sleep(10);
            System.out.println(" 子线程 threadId: " + Thread.currentThread().getId() + ",threadName: " + Thread.currentThread().getName());
            return "Callable f1";
        };
    }


}
